
// -----------------------------------------------------------------------------

#include "stdafx.h"
#include "Win32.IO.TextFile.h"
#include "Win32.IO.File.h"
#include "Win32.Memory.h"

namespace Kif
{
namespace Win32
{
namespace IO
{

// -----------------------------------------------------------------------------

/// <summary>read file and place content into a text buffer.</summary>
TextBuffer<char> *TextFile::ReadTextFile(TCHAR *path)
{
   TextBuffer<char> *tBuff = new TextBuffer<char>();
   if (tBuff == NULL)
   {
      return(NULL);
   }

   Kif::Win32::IO::File file;
   tBuff->FilePath.Copy(path);
   tBuff->BufferLength = file.FileSize(path);

   TextFile tFile;
   tFile.OpenRead(path);

   DWORD readBytes;
   tBuff->ptrRawBuffer = tFile.ReadRawBuffer(tBuff->BufferLength,&readBytes);
   if (tBuff->ptrRawBuffer == NULL)
   {
      tBuff->LastResult = *tFile.GetLastResult();
      return(tBuff);
   }

   return(tBuff);
}  // end of ReadTextFile

// -----------------------------------------------------------------------------

/// <summary>read data in give buffer.</summary>
/// <remarks>the following only reads AnsiStrings (char size) strings.
/// Caller will be responsable for diposing the "raw buffer" returned by
/// this function</remarks>
char *TextFile::ReadRawBuffer(DWORD bufferSizeInBytes, DWORD *bytesRead)
{
   // make sure input data is ok
   if ((bytesRead == NULL) || (bufferSizeInBytes <= 0))
   {
      lastResult.ErrorNo = ERROR_INVALID_DATA;
      return(NULL);
   }
   *bytesRead = 0;

   // prepare buffer to receive data

   char *charBuffer = (char*)Kif::Win32::Memory::alloc(bufferSizeInBytes+1);
   if (charBuffer == NULL)
   {
      lastResult.ErrorNo = ERROR_NOT_ENOUGH_MEMORY;
      return(NULL);
   }
   *(charBuffer+bufferSizeInBytes) = NULL;

   BOOL done;

   try
   {
      done = ReadFile(hFile,
         charBuffer,
         bufferSizeInBytes,
         bytesRead, NULL);

      if (!done)
         lastResult.ErrorNo = GetLastError();

   }
   catch (TCHAR *ex)
   {
      done = false;
      lastErrorMessage = ex;
   }

   // is there anything else to read?
   if (done && (bytesRead == 0))
      return(NULL);
   else
      return(charBuffer);

}  // end of ReadRawBuffer

// -----------------------------------------------------------------------------

/// <summary>read data in give buffer.</summary>
/// <remarks>the following only reads AnsiStrings (char size) strings.
/// </remarks>
DWORD TextFile::Read(Kif::Win32::String &buffer)
{
   // prepare buffer to receive data

   DWORD bytesRead;
   DWORD buffBytes =
      ((DWORD)buffer.GetLength() * sizeof(char)) + sizeof(char);

   char *charBuffer = (char*)Kif::Win32::Memory::alloc(buffBytes);
   if (charBuffer == NULL)
   {
      lastResult.ErrorNo = ERROR_NOT_ENOUGH_MEMORY;
      return(-1);
   }

   BOOL done;

   try
   {
      done = ReadFile(hFile,
         charBuffer,
         buffBytes,
         &bytesRead, NULL);

      if (!done)
         lastResult.ErrorNo = GetLastError();
      else
      if (bytesRead > 0)
         buffer.Overwrite(charBuffer,bytesRead/sizeof(char));

   }
   catch (TCHAR *ex)
   {
      done = false;
      lastErrorMessage = ex;
   }

   Kif::Win32::Memory::free(charBuffer);

   // is there anything else to read?
   if (done && (bytesRead == 0))
      return(0);
   else
      return(bytesRead);

}  // end of Read

// -----------------------------------------------------------------------------

/// <summary>write data to file.</summary>
/// <remarks>the following only reads AnsiStrings (char size) strings.
/// </remarks>
DWORD TextFile::Write(BYTE *bytes,DWORD bytesLength)
{
   DWORD bytesWriten = 0;
   BOOL done;

   try
   {
      done = WriteFile(hFile,
         bytes,
         bytesLength,
         &bytesWriten, NULL);

      if (!done)
         lastResult.ErrorNo = GetLastError();
   }
   catch (TCHAR *ex)
   {
      done = false;
      lastErrorMessage = ex;
   }

   if (!done)
      bytesWriten = 0;

   return(bytesWriten);
}  // end of Write

}  // end of Kif::Win32::IO
}  // end of Kif::Win32
}  // end of Kif

